<template>
	<view :style="themeColor">
		<view class="page">
			<view class="flex flex-direction flex-wrap align-stretch benben-position-layout flex regions_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<view class='flex flex-wrap align-center justify-between regions_fd0_0'>
					<view class='flex flex-wrap align-center regions_fd0_0_c0' @tap.stop="handleJumpDiy"
						data-type="back" data-url="1">
						<text class='fu-iconfont2  regions_fd0_0_c0_c0'>&#xE794;</text>
					</view>
					<view class='flex flex-wrap align-center'>
						<text class='regions_fd0_0_c1_c0'>选择服务区域</text>
					</view>
					<view class='flex align-center justify-end regions_fd0_0_c0'>
						<text class='regions_fd0_0_c2_c0' @tap.stop="ejectionFunc()"
							v-if="appSystemIdentification!='Wechat'">确定</text>
					</view>
				</view>

			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			<!---flex布局flex布局开始-->
			<view class="flex benben-flex-layout flex-wrap align-center regions_flex_1">
				<text class='regions_fd1_0'>当前位置</text>
			</view>

			<!---flex布局flex布局结束-->
			<!---flex布局flex布局开始-->
			<view class="flex flex-wrap align-center justify-between benben-flex-layout regions_flex_2">
				<view class='flex flex-wrap align-center regions_fd2_0'>
					<image class='regions_fd2_0_c0' mode="aspectFit" :src='STATIC_URL+"553.png"'></image>
					<text class='regions_fd2_0_c1'>{{cityinfo.text || '未知'}}</text>
				</view>
				<view class='flex flex-wrap align-center' @tap.stop="handleJumpDiy" data-type="navigateTo"
					:data-url="`/pages/my/selectCity/selectCity`">
					<text class='regions_fd2_1_c0'>可切换</text>
					<image class='regions_fd2_1_c1' mode="aspectFit" :src='STATIC_URL+"554.png"'></image>
				</view>
			</view>

			<!---flex布局flex布局结束-->
			<!---flex布局flex布局开始-->
			<view class="flex benben-flex-layout flex-wrap align-center regions_flex_3">
				<text class='regions_fd3_0'>选择服务区域</text>
			</view>

			<!---flex布局flex布局结束-->
			<!---flex布局flex布局开始-->
			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout regions_flex_4">
				<benben-select-diy ref="showSelectPopup1692326281449"
					class-name='flex flex-direction align-stretch flex' :items.sync="queryServiceArea"
					v-model="region.aid" :label.sync="region.name" @change="changeCityFunc()" default-type="aid"
					default-label="name" :allow-cancel='true' type="checkbox" :disabled='false' :max-select="-1">
					<template v-for='(item,key0) in queryServiceArea'>
						<view v-if="item.isSelected" class='flex align-center justify-between flex regions_fd4_0_c0'
							:key="key0" @tap="$refs.showSelectPopup1692326281449.tapHandle(key0)">
							<text class='flex-sub regions_fd4_0_c0_c0'>{{item.name}}</text>
							<image class='regions_fd4_0_c0_c1' mode="aspectFit" :src='STATIC_URL+"133.png"'></image>
						</view>
						<view v-else class='flex align-center justify-between flex regions_fd4_0_c0' :key="key0"
							@tap="$refs.showSelectPopup1692326281449.tapHandle(key0)">
							<text class='flex-sub regions_fd4_0_c0_c0'>{{item.name}}</text>
							<image class='regions_fd4_0_c0_c1' mode="aspectFit" :src='STATIC_URL+"331.png"'></image>
						</view>
					</template></benben-select-diy>
				<benben-empty :list-data="queryServiceArea">
					<view class='flex flex-direction flex-wrap align-center regions_fd4_1'>
						<image class='regions_fd4_1_c0' mode="widthFix" :src='STATIC_URL+"62.png"'></image>

					</view>
				</benben-empty>
			</view>

			<!---flex布局flex布局结束-->
			<view class="flex flex-wrap align-start justify-center benben-position-layout flex skill_flex_2"
				v-if=" appSystemIdentification=='Wechat'">
				<button class='skill_fd2_0' @tap.stop="ejectionFunc()">确定</button>
			</view>
			<view :style="{height: '120rpx'}" v-if=" appSystemIdentification=='Wechat'"></view>
		</view>
	</view>
</template>
<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	import amap from '@/libs/amap-wx.js'
	export default {
		components: {},


		data() {
			return {
				"queryServiceArea": [],
				"skill_label": "",
				"region": {
					"aid": "",
					"name": ""
				},
				"cityinfo": {
					"text": "",
					"lng": "",
					"lat": ""
				},
				"obj": {},
				"id": "",
				"amapPlugin": "",
			};
		},
		computed: {
			themeColor() {
				return this.$store.getters.themeColor
			},
			/**
			 * @returns 
			 * Android       安卓
			 * IOS       苹果
			 * Web       H5 或 pc
			 * Wechat       微信小程序
			 * Alipay       支付宝小程序
			 * Baidu       百度小程序
			 * ByteBounce       抖音小程序
			 * 360       360小程序
			 * FastApp       快应用
			 */
			appSystemIdentification() {
				return this.$store.state.appSystemIdentification
			}
		},
		watch: {},
		onLoad(options) {
			let {
				id
			} = options
			if (id !== undefined) this.id = id
			this.getLocationDataFuncFunc()
			this.diyPageListen()
			this.getAmapFunc()
		},
		onUnload() {
			this.diyOffPageListen()
		},
		onReady() {

		},
		onShow() {

		},
		onHide() {

		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {
			//查询服务区域
			async getQueryServiceAreaFunc() {
				//请求方法
				//数据验证

				let dataqueryServiceArea = await this.$api.post(global.apiUrls.post64dc7e578e808, {
					city: this.cityinfo.text
				});

				if (dataqueryServiceArea.data.code != 1) {
					this.$message.info(dataqueryServiceArea.data.msg);
					return
				}
				let infoqueryServiceArea = dataqueryServiceArea.data;
				this.queryServiceArea = infoqueryServiceArea.data

				this.$nextTick(() => {
					this.region.aid = this.region.aid + '\xa0'
				})
			},
			//抛出
			// ejectionFunc() {
			// 	this.region.name = ''
			// 	for (let key in this.obj) {
			// 		this.region.name += this.obj[key] + ','
			// 	}
			// 	uni.$emit('serviceData', this.region)
			// 	this.$urouter.navigateBack(1);
			// },
			//抛出
			ejectionFunc() {
				if (Object.keys(this.obj).length != 0) {
					this.region.name = ''
				}
				for (let key in this.obj) {
					this.region.name += this.obj[key] + ','
				}
				uni.$emit('serviceData', this.region)
				this.$urouter.navigateBack(1);
			},
			//获取自动定位
			relocateFunc() {
				if (!this.setCheckPermission(1)) {
					return
				}
				uni.showLoading({
					title: '获取位置中'
				})
				let that = this
				uni.getLocation({
					geocode: true,
					type: 'gcj02',
					success: (res) => {
						if (that.appSystemIdentification == 'Android') {
							uni.hideLoading();
							// that.cityinfo.text = res.address.city || res.address.province
							that.cityinfo.text = (res.address.city && res.address.city.length >  0) ? res.address.city : datas.province;
							that.cityinfo.lat = res.latitude
							that.cityinfo.lng = res.longitude
							console.log(res, "这是事实少时诵诗书少时诵诗书是撒是撒是撒是撒是撒是撒是撒是撒");
							uni.setStorageSync('cityinfo', that.cityinfo)
							this.getQueryServiceAreaFunc()
						} else if (that.appSystemIdentification == 'Wechat' || that.appSystemIdentification ==
							'IOS') {
							console.log(that.amapPlugin);
							that.amapPlugin.getRegeo({
								location: `${res.longitude},${res.latitude} `,
								success: (addr) => {
									uni.hideLoading();
									let datas = addr[0].regeocodeData.addressComponent
									// that.cityinfo.text = datas.city || datas.province;
									that.cityinfo.text = (datas.city && datas.city.length >  0) ? datas.city : datas.province;
									that.cityinfo.lat = res.latitude;
									that.cityinfo.lng = res.longitude;
									uni.setStorageSync('cityinfo', that.cityinfo)
									this.getQueryServiceAreaFunc()
								},
								fail: (err) => {
									uni.hideLoading();
									console.log(err, 'err');
								}
							});
						}
					},
					fail: (error) => {
						console.log(error, 'error');
						uni.hideLoading();
					}
				});
			},
			//获取首页储存位置
			getLocationDataFuncFunc() {
				this.region.aid = this.id
				let that = this
				if (uni.getStorageSync('cityinfo')) {
					that.cityinfo = uni.getStorageSync('cityinfo')
					that.getQueryServiceAreaFunc()
				} else {
					that.relocateFunc()
				}
			},
			//改变服务区域
			changeCityFunc() {
				if (!Object.keys(this.obj).length) {
					this.obj[this.cityinfo.text] = this.region.name
				} else {
					for (let key in this.obj) {
						console.log(key, this.obj[key])
						if (key == this.cityinfo.text) {
							this.obj[key] = this.region.name
						} else {
							this.obj[this.cityinfo.text] = this.region.name
						}
					}
				}
			},
			//new地图
			getAmapFunc() {
				this.amapPlugin = new amap.AMapWX({
					key: "f5aa0e7e4b38444272b0a895e91de9ce"
				});
				console.log(this.amapPlugin);
			},
			//设置区域
			setdistrictFunc(data) {
				if (this.cityinfo.text != data.text) {
					this.region.aid = ''
				}
				this.cityinfo = data;
				this.getQueryServiceAreaFunc()
			},
			//页面监听
			diyPageListen() {
				uni.$on('cityinfo', (data) => {
					// this.cityinfo = data;
					this.setdistrictFunc(data)
				});
			},
			//注销页面监听
			diyOffPageListen() {
				uni.$off('cityinfo');
			}
		}
	};
</script>
<style lang="scss" scoped>
	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #fff;
		background-size: 100% auto;
	}

	.regions_flex_0 {
		border-bottom: 1px solid #eee;
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.regions_fd0_0_c2_c0 {
		color: #333333;
		font-size: 32rpx;
		font-weight: 500;
		line-height: 45rpx;
	}

	.regions_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333333;
	}

	.regions_fd0_0_c0_c0 {
		font-size: 36rpx;
		color: #333;
		font-weight: 500;
	}

	.regions_fd0_0_c0 {
		width: 120rpx;
		height: 88rpx;
	}

	.regions_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
		height: 88rpx;
	}

	.regions_flex_1 {
		background: rgba(248, 248, 248, 1);
		background-size: 100% auto;
		padding: 24rpx 32rpx 24rpx 32rpx;
	}

	.regions_fd1_0 {
		color: #666666;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 33rpx;
	}

	.regions_flex_2 {
		padding: 24rpx 32rpx 24rpx 32rpx;
	}

	.regions_fd2_1_c1 {
		width: 22rpx;
		height: 12rpx;
		margin: 0rpx 0rpx 0rpx 24rpx;
	}

	.regions_fd2_1_c0 {
		color: #333333;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 45rpx;
		margin: 0rpx 0rpx 0rpx auto;
	}

	.regions_fd2_0_c1 {
		color: #333333;
		font-size: 28rpx;
		font-weight: 400;
		line-height: 40rpx;
	}

	.regions_fd2_0_c0 {
		width: 20rpx;
		height: 24rpx;
		margin: 0rpx 10rpx 0rpx 0rpx;
	}

	.regions_fd2_0 {
		border: 1px solid var(--benbenbdColor2);
		background: rgba(227, 29, 26, 0.1);
		padding: 0rpx 57rpx 0rpx 57rpx;
		background-size: 100% auto;
		border-radius: 10rpx;
		height: 72rpx;
		line-height: 72rpx;
	}

	.regions_flex_3 {
		background: rgba(248, 248, 248, 1);
		background-size: 100% auto;
		padding: 24rpx 32rpx 24rpx 32rpx;
	}

	.regions_fd3_0 {
		color: #666666;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 33rpx;
	}

	.regions_flex_4 {
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.regions_fd4_1_c0 {
		width: 400rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.regions_fd4_1 {
		width: 100%;
		padding: 50rpx 0rpx 50rpx 0rpx;
	}

	.regions_fd4_0_c0_c1 {
		width: 40rpx;
		height: 40rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.regions_fd4_0_c0_c0 {
		color: #333333;
		font-size: 32rpx;
		font-weight: 400;
		line-height: 45rpx;
		margin: 0rpx 24rpx 0rpx 0rpx;
		-webkit-line-clamp: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
	}

	.regions_fd4_0_c0 {
		border-bottom: 1px solid #eee;
		padding: 32rpx 0rpx 32rpx 0rpx;
	}

	//按钮样式
	.skill_flex_2 {
		width: 750rpx;
		height: 120rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
	}

	.skill_fd2_0 {
		background: #E31D1A;
		border-radius: 44rpx;
		width: 686rpx;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #fff;
		height: 88rpx;
	}
</style>